package com.xidian.zhiancloud.controller.accidentManage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xidian.zhiancloud.constant.AccidentManagementConstant;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.AccidentReviewDTO;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.AccidentMessage;
import com.xidian.zhiancloud.model.AccidentReview;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.accidentmessage.AccidentMessageQO;
import com.xidian.zhiancloud.service.accidentManage.AccidentMessageService;
import com.xidian.zhiancloud.service.accidentManage.AccidentReviewService;
import com.xidian.zhiancloud.service.user.UserService;
import com.xidian.zhiancloud.service.user.WorkFlowUserService;
import com.xidian.zhiancloud.service.user.usermanage.OrganizationService;
import com.xidian.zhiancloud.util.zyf.ExcelUtils;
import com.xidian.zhiancloud.vo.accidentmessage.AccidentHandleExportVO;
import com.xidian.zhiancloud.vo.accidentmessage.AccidentMessageVO;
import com.xidian.zhiancloud.vo.accidentmessage.AccidentReviewExportVO;
import com.xidian.zhiancloud.vo.accidentmessage.AccidentReviewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AccidentReviewController
 * @Description 事故审核信息管理
 * @Author zc
 **/
@RestController
@RequestMapping("/accidentReview")
public class AccidentReviewController extends BaseController {

    @Autowired
    private AccidentReviewService accidentReviewService;

    @Autowired
    private AccidentMessageService accidentMessageService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private WorkFlowUserService workFlowUserService;
    /**
     * 添加事故审核信息
     * @param accidentReview 事故审核信息
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> addAccidentReview(AccidentReview accidentReview){

        accidentReview.setCompanyId(getSessionUserCompanyId());
        accidentReview.setCreateTime(new Date(System.currentTimeMillis()));
        accidentReview.setCreator(getSessionUserid());
        return ResultDTO.successOf(accidentReviewService.addAccidentReviewMsg(accidentReview),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除事故审核信息
     * @param ids 事故id
     * @return 返回是否删除成功
     */
    @PostMapping("/delete")
    public ResultDTO<Boolean> deleteByIds(Long[] ids){
        accidentReviewService.removeByIds(Arrays.asList(ids));
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据事故id获取事故审核信息
     * @param id 事故id
     * @return 返回事故审核信息
     */
    @GetMapping("/getById")
    public ResultDTO<List<AccidentReviewVO>> getById(@RequestParam("id") Long id){
        List<AccidentReviewVO> accidentReviewVOList = accidentReviewService.getAccidentReviewById(id);
        return ResultDTO.successOf(accidentReviewVOList,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 修改事故审核信息
     * @param accidentReviewVO 传入事故审核信息实体
     * @return 返回是否添加成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(AccidentReviewVO accidentReviewVO){
        //判断当前人是否有操作权限
        List<Long> ids = workFlowUserService.getAccidentAudit(getSessionUserCompanyId());
        Long currentUserId = getSessionUserid();
        if(!ids.contains(currentUserId)){
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        accidentReviewVO.setCompanyId(null);
        accidentReviewVO.setCreator(null);
        accidentReviewVO.setCreateTime(null);
        accidentReviewService.updateAccidentReviewMsg(accidentReviewVO);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 条件查询
     * @param accidentMessageQO 请求参数
     * @return 返回事故审核基本信息列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<AccidentMessageVO>> getListByQO(AccidentMessageQO accidentMessageQO) {
        if(accidentMessageQO.getCurrent() == null || accidentMessageQO.getSize() == null){
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }

        accidentMessageQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(accidentReviewService.getListByQO(accidentMessageQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 导出
     * @param response
     * @param ids 事故id
     * @throws IOException
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,Long[] ids) throws IOException{
        if(ids == null || ids.length==0){
            return;
        }

        List<AccidentMessage> list = accidentMessageService.listByIds(Arrays.asList(ids));
        List<AccidentReviewExportVO> new_list = new ArrayList<>();

        list.forEach(obj -> {
            AccidentReviewExportVO vo = new AccidentReviewExportVO();
            BeanUtils.copyProperties(obj,vo);

            SysUser creator = userService.getById(vo.getCreator());
            vo.setCreatorName(creator==null ? "该用户不存在" : creator.getName());

            Organization org = organizationService.getById(vo.getDepartmentId());
            vo.setDepartmentName(org==null ? "该部门不存在" : org.getName());

            LambdaQueryWrapper<AccidentReview> query = Wrappers.lambdaQuery();
            query.eq(AccidentReview::getAccidentId,vo.getId());
            List<AccidentReview> reviewList = accidentReviewService.list(query);
            List<AccidentReviewVO> reviewVOList = new ArrayList<>();

            reviewList.forEach(entry -> {
                AccidentReviewVO reviewVO = new AccidentReviewVO();
                BeanUtils.copyProperties(entry,reviewVO);

                SysUser user = userService.getById(reviewVO.getReviewerId());
                reviewVO.setReviewName(user==null ? "该用户不存在" : user.getName());

                Organization organization = organizationService.getById(reviewVO.getDepartId());
                reviewVO.setReviewDepartment(organization==null ? "该部门不存在":organization.getName());

                reviewVO.setResultStr(AccidentManagementConstant.Result.get(reviewVO.getResult()));

                reviewVOList.add(reviewVO);
            });

            vo.setAccidentReviewVOList(reviewVOList);

            new_list.add(vo);
        });

        ExcelUtils.exportExcel(new_list,"事故审核","事故审核",AccidentReviewExportVO.class,"事故审核",response);
    }
}
